<?php
/**
 * 
 * @author Freezy
 */
class Karyawan extends MY_Controller {

	function Karyawan(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function index(){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array('view'=>'template/horz_nav', 
											'view_data'=>array('data'=>$CONF['nav']['psdm']));
		$layoutConf['right_content'] = array('view'=>'karyawan/overview', 
											'view_data'=>$CONF['search']);
		$this->initialize_layout($layoutConf);
		
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'] = array_merge(
		$pageConf['jscript_default'],
			array('psdm/karyawan', 'utils/searchDialog','datepicker'));
		$pageConf['jscript'] = $pageConf['jscript_default'];
		
		$pageConf['css_default'] = array_merge($pageConf['css_default'], array('datepicker'));
		$pageConf['css'] = $pageConf['css_default'];
		
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
	}
	
	function _search(){
		$this->load->model('karyawan_model');

		$key = $this->input->post('key');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$table_content = "";
		$row = 0;
		$data = $this->karyawan_model->select_last_entry($key, '', $limit, $offset, $orderby);
		$table_content = $this->load->view('karyawan/table_content', array('data'=>$data),true); 	
		echo json_encode(array(
			'table_content'=> $table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->karyawan_model->count_record()
		));
	}
	
	function _addModal(){
		$this->load->model('karyawan_model');
		$this->load->model('karyawan_status_model');
		$this->load->model('kota_model');
		$this->load->model('departemen_model');
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		$data = array( 
			'agama_options'=>$CONF['karyawan']['agama_options'],
			'sts_data'=> $this->karyawan_status_model->select_all(array('kary_sts_id'=>'asc')),
			'kota_data'=> $this->kota_model->select_all(array('kota_nama'=>'asc')),
			'dep_data'=> $this->departemen_model->select_all(array('dep_nama'=>'asc'))
		);
		
		$this->finalize_view_str(
			$this->load->view('karyawan/add',$data, true)
		);
	}
	
	function _save(){
		$this->load->model('karyawan_model');
		$inp = $this->input->post('inp');
		$res = true;

		//save
		$res &= $this->karyawan_model->insert( array(
			'kary_id' => $inp[0],
			'kary_nama' => $inp[1],
			'kary_tempat_lahir' => $inp[2],
			'kary_tgl_lahir' => $inp[3],
			'kary_jenis_kelamin' => $inp[4],
			'kary_golongan_darah' => $inp[5],
			'kary_status_marital' => $inp[6],
			'kary_jml_tanggungan' => $inp[15],
			'kary_agama' => $inp[7],
			'kary_email' => $inp[8],
			'kary_tgl_masuk' => $inp[9],
			'kary_tgl_keluar' => '',
			'kary_sts_id' => $inp[10]
		));
		
		//create user
		$this->load->model('login_model');
		$this->login_model->insert(array(
			'usrname'=>$inp[0],
			'passwd'=>md5($inp[0])
		));
		
		//alamat
		$this->load->model(array('karyawan_alamat_model', 'kota_model', 'propinsi_model'));
		foreach ($inp[11] as $kk){
			$kota_data = $this->kota_model->select_last_entry(false, "kota_id='".$kk[2]."'");
			$kota_data = $kota_data->row();
			
			$res &= $this->karyawan_alamat_model->insert(array(
				//'kary_alm_id' => '',
				'kary_alm_alamat' => $kk[1],
				'kary_alm_kode_pos' => $kota_data->kota_kode_pos,
				'kary_alm_kode_area' => $kota_data->kota_kode_area_telepon,
				'kary_alm_kota' => $kota_data->kota_nama,
				'kary_alm_propinsi' => $kota_data->propinsi_nama,
				'kary_id' => $inp[0]
			));
		}
		
		//telepon
		$this->load->model('karyawan_telepon_model');
		foreach ($inp[12] as $kk){
			$res &= $this->karyawan_telepon_model->insert(array(
				'kary_tlp_no'=> $kk[1].'-'.$kk[2],
				'kary_id'=> $inp[0]
			));
		}
		
		//create default jabatan
		$this->load->model(array('karyawan_history_jabatan_model','jabatan_model'));
		$this->karyawan_history_jabatan_model->insert(array_combine(
			array('kary_jbt_tgl_menjabat','kary_id','jbt_id'),
			array(date("Y-m-d H:i:s",time()), $inp[0], $this->jabatan_model->getDefaultJabatanFor($inp[100]))
		));
		
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->karyawan_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _editModal($id){
		$this->load->model(array('karyawan_model', 'karyawan_status_model', 'karyawan_alamat_model', 
			'karyawan_telepon_model','kota_model'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		
		$result = $this->karyawan_model->select_by_id($id);
		$result = (array)$result->row();
		$kary = array(
			'agama_options'=>$CONF['karyawan']['agama_options'],
			'sts_data' => $this->karyawan_status_model->select_all(array('kary_sts_id'=>'asc')),
			'alamat_data' => $this->karyawan_alamat_model->select_by_karyawan($result['kary_id'], array('kary_alm_alamat'=>'asc')),
			'tlp_data' => $this->karyawan_telepon_model->select_by_karyawan($result['kary_id'], array('kary_tlp_no'=>'asc')),
			'kota_data' => $this->kota_model->select_all( array('kota_nama'=>'asc') )
		);
		
		$this->finalize_view_str(
			$this->load->view('karyawan/edit', array_merge($kary, $result), true)
		);
	}
	
	function _update(){
		$this->load->model('karyawan_model');
		$inp = $this->input->post('inp');
		$res = true;

		//save
		$res &= $this->karyawan_model->update( array('kary_id' => $inp[0]), array(
			'kary_nama' => $inp[1],
			'kary_tempat_lahir' => $inp[2],
			'kary_tgl_lahir' => $inp[3],
			'kary_jenis_kelamin' => $inp[4],
			'kary_golongan_darah' => $inp[5],
			'kary_status_marital' => $inp[6],
			'kary_jml_tanggungan' => $inp[15],
			'kary_agama' => $inp[7],
			'kary_email' => $inp[8],
			'kary_tgl_masuk' => $inp[9],
			'kary_tgl_keluar' => '',
			'kary_sts_id' => $inp[10]
		));
		
		//alamat
		$this->load->model(array('karyawan_alamat_model', 'kota_model', 'propinsi_model'));
		foreach ($inp[11] as $kk=>$vv){
			$kota_data = $this->kota_model->select_last_entry(false, "kota_id='".$vv[2]."'");
			$kota_data = $kota_data->row();
			
			//alamat yang telah ada di-record, ditandai dengan "record-".
			//cek ada atau tidak tanda tesebut
			$count=0;
			str_replace("record-","",$kk,$count);
			
			//ditemukan tanda, update datanya
			if ($count>0){
				$res &= $this->karyawan_alamat_model->update(array('kary_alm_id' => str_replace("record-","",$kk)), array(
					'kary_alm_alamat' => $vv[1],
					'kary_alm_kode_pos' => $kota_data->kota_kode_pos,
					'kary_alm_kode_area' => $kota_data->kota_kode_area_telepon,
					'kary_alm_kota' => $kota_data->kota_nama,
					'kary_alm_propinsi' => $kota_data->propinsi_nama,
					'kary_id' => $inp[0]
				));
			}
			//tidak ditemukan tanda, insert datanya
			else{	
				$res &= $this->karyawan_alamat_model->insert(array(
					'kary_alm_alamat' => $vv[1],
					'kary_alm_kode_pos' => $kota_data->kota_kode_pos,
					'kary_alm_kode_area' => $kota_data->kota_kode_area_telepon,
					'kary_alm_kota' => $kota_data->kota_nama,
					'kary_alm_propinsi' => $kota_data->propinsi_nama,
					'kary_id' => $inp[0]
				));		
			}
		}
		
		//telepon
		if (isset($inp[12])){
			$this->load->model('karyawan_telepon_model');
			foreach ($inp[12] as $kk){
				$res &= $this->karyawan_telepon_model->insert(array(
					'kary_tlp_no'=> $kk[1].'-'.$kk[2],
					'kary_id'=> $inp[0]
				));			
			}
		}
		
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->karyawan_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _delete($id){
		$this->load->model('karyawan_model');
		$res = $this->karyawan_model->delete(array('kary_id' => $id));
		$msg = $res? "Proses hapus selesai" : "Proses hapus gagal, ".$this->karyawan_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _setStatusModal($kary_id, $kary_nama=false){
		$this->load->model('karyawan_status_model');
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		$arr = array(
			'cuti_options'=>$CONF['karyawan']['cuti_options'],
			'sts_data' => $this->karyawan_status_model->select_all(array('kary_sts_nama'=>'asc')),
			'kary_id' => $kary_id,
			'kary_nama' => $kary_nama
		);
		
		$this->finalize_view_str(
			$this->load->view('karyawan/addStatus',$arr, true)
		);
	}
	
	function _saveStatus(){
		$this->load->model('karyawan_model');
		$inp = $this->input->post('inp');
		
		//update status karyawan
		$res = array( 'kary_sts_id' => $inp[1] );
		if ($inp[1] == "NA"){
			//update juga tgl non aktifnya
			$res['kary_tgl_keluar'] = date('Y-m-d',time());
		}
		$res = $this->karyawan_model->update(array('kary_id' => $inp[0]), $res);
		
		//update history cuti
		if ($inp[1] == "CT"){
			$this->load->model('karyawan_history_cuti_model');
			$res &= $this->karyawan_history_cuti_model->insert( array(
				'kary_cuti_jenis' => $inp[2][1],
				'kary_cuti_tgl_mulai' => $inp[2][2],
				'kary_cuti_tgl_selesai' => $inp[2][3],
				'kary_cuti_keterangan' => $inp[2][4],
				'kary_id' => $inp[0]
			));
		}
		
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->karyawan_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _setJabatanModal($kary_id, $kary_nama, $jbt_id, $jbt_nama){
		$arr = array(
			'kary_id' => $kary_id,
			'kary_nama' => $kary_nama,
			'jbt_id' => $jbt_id, 
			'jbt_nama' => $jbt_nama
		);
		
		$this->finalize_view_str(
			$this->load->view('karyawan/addJabatan', $arr, true)
		);
	}
	
	function _saveJabatan(){
		$this->load->model('karyawan_history_jabatan_model');
		$inp = $this->input->post('inp');
		$res = $this->karyawan_history_jabatan_model->insert( array(
//			'kary_jbt_id' => 	 	 	
			'kary_jbt_tgl_menjabat'	=> $inp[2],
			'kary_jbt_tgl_berhenti'	=> $inp[3],
			'kary_id' => $inp[0],
			'jbt_id' => $inp[1]
		));
			
		$msg = $res? "Proses simpan selsai" : "Proses simpan gagal, ".$this->karyawan_history_jabatan_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _historyJabatan($kary_id){
		$this->load->model('karyawan_model');
		$this->load->model('karyawan_history_jabatan_model');
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		
		$key = $this->input->post('key');
		$limit = $CONF['search']['limit'];
		$offset = $this->input->post('offset') * $limit;
		$orderby = array('kary_jbt_tgl_menjabat'=>'asc','kary_jbt_tgl_berhenti'=>'asc');
		
		$data = $this->karyawan_history_jabatan_model->getHistory2($kary_id, $key, false, $limit, $offset, $orderby);
		$arr = array( 'kary_id' => $kary_id, 'data'=>$data );
		
		$this->finalize_view_str(
			$this->load->view('karyawan/historyJabatan', $arr, true)
		);
	}
	
	function _historyJabatan_search(){
		$this->load->model('karyawan_history_jabatan_model');

		$key = $this->input->post('key');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$kary_id = $this->input->post('kary_id');
		
		$table_content = "";
		$row = 0;
		$data = $this->karyawan_history_jabatan_model->getHistory2($kary_id, $key, '', $limit, $offset, $orderby);
		$table_content = $this->load->view('karyawan/historyJabatan_tableContent', array('data'=>$data),true); 	
		echo json_encode(array(
			'table_content'=> $table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->karyawan_history_jabatan_model->count_record() 
		));
	}
	
	function _historyCuti($kary_id){
		$this->load->model('karyawan_history_cuti_model');
		
		$data = $this->karyawan_history_cuti_model->getHistory($kary_id);
		$arr = array( 'kary_id' => $kary_id, 'data'=>$data );
		
		$this->finalize_view_str(
			$this->load->view('karyawan/historyCuti', $arr, true)
		);
	}
	
	function _historyCuti_search(){
		$this->load->model('karyawan_history_cuti_model');

		$key = $this->input->post('key');
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$kary_id = $this->input->post('kary_id');
		
		$table_content = "";
		$row = 0;
		$data = $this->karyawan_history_cuti_model->getHistory($kary_id, $key, '', $limit, $offset, $orderby);
		$table_content = $this->load->view('karyawan/historyCuti_tableContent', array('data'=>$data),true); 	
		echo json_encode(array(
			'table_content'=> $table_content,
			'table_num_record'=> $key? $data->num_rows() :$this->karyawan_history_cuti_model->count_record() 
		));
	}
	
	//******************//
	//		MISC		//
	//******************//
	
	function _kotaCombo(){
		$this->load->model('kota_model');
		$result = $this->kota_model->select_all( array('kota_nama'=>'asc') );
		$vv="";
		foreach($result->result_array() as $row){
			$vv .= "<option value='". $row['kota_id'] ."'>".$row['kota_nama']."</option>";
		}
		
		$this->finalize_view_str($vv);
	}
	
	/**
	 * Delete alamat dalam record database.
	 * Function ini dipanggil saat melakukan penghapusan alamat pada form edit karyawan 
	 * @param $kary_alm_id - id alamat
	 */
	function _deletePiecesAlamat($kary_alm_id){
		$this->load->model('karyawan_alamat_model');
		
		//unformat first, the input look like this "record-[id]"
		$id = str_replace("record-", "", $kary_alm_id);
		$this->karyawan_alamat_model->delete(array('kary_alm_id' => $id));
	}
	
	function _deletePiecesTlp($kary_tlp_no){
		$this->load->model('karyawan_telepon_model');
		
		//unformat first, the input look like this "record-[id]"
		$id = str_replace("record-", "", $kary_tlp_no);
		$this->karyawan_telepon_model->delete(array('kary_tlp_no' => $id));
	}
	
	function _validateID($id){
		$this->load->model('karyawan_model');
		$check = $this->karyawan_model->select_by_id($id);
		if ($check = (array)$check->row()){
			echo json_encode(array('error'=>true,'msg'=>"ID telah digunakan oleh karyawan '".$check['kary_nama']."'"));
		}
		else{
			echo json_encode(array('error'=>false,'msg'=>""));
		}
	}
}
/* End of file psdm/karyawan.php */
/* Location: ./system/application/controllers/psdm/karyawan.php */